package net.solvetheriddle.wwclient.entity;

import java.util.ArrayList;
import java.util.List;

import net.solvetheriddle.wwclient.WWLogic;

public class Project {
  private static final long ONE_DAY = 1000 * 60 * 60 * 24;
  private static final long ONE_WEEK = ONE_DAY * 7;
  private static final long ONE_MONTH = ONE_DAY * 30;

  private static final int DAY_STATISTICS = 0;
  private static final int WEEK_STATISTICS = 1;
  private static final int MONTH_STATISTICS = 2;
  private static final int COMPLETE_STATISTICS = 3;

  private long id;
  private String name;
  private boolean active;
  private boolean open;
  private List<Work> works;

  public Project(long id, String name, List<Work> works, boolean open) {
    this.id = id;
    this.name = name;
    this.active = false;
    this.works = works;
    this.open = open;
  }

  public Project(long id, String name, boolean open) {
    this.id = id;
    this.name = name;
    this.open = open;
    this.works = new ArrayList<Work>();
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<Work> getWorks() {
    return works;
  }

  public void setWorks(List<Work> works) {
    this.works = works;
  }

  public void addWork(Work work) {
    works.add(work);
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean active) {
    this.active = active;
  }

  public boolean isOpen() {
    return open;
  }

  public void setOpen(boolean open) {
    this.open = open;
  }

  public String toString() {
    StringBuilder s = new StringBuilder(name);
//    if (open) {
//      s.append(" [").append("open").append("]");
//    }
    return s.toString();
  }

  @Override
  public boolean equals(Object o) {
    if (o instanceof Project) {
      Project p = (Project) o;
      return p.getId() == this.id;
    }
    return false;
  }

  @Override
  public int hashCode() {
    return super.hashCode() + 6 * (int) this.id;
  }

  public float countWorkingHours(int timePeriod) {
    float hoursWorked = 0;

    long countFrom = 0;
    switch (timePeriod) {
    case DAY_STATISTICS:
      countFrom = WWLogic.getCurrentMillis() - ONE_DAY;
      break;
    case WEEK_STATISTICS:
      countFrom = WWLogic.getCurrentMillis() - ONE_WEEK;
      break;
    case MONTH_STATISTICS:
      countFrom = WWLogic.getCurrentMillis() - ONE_MONTH;
      break;
    case COMPLETE_STATISTICS:
      countFrom = 0;
      break;
    default:
      throw new IllegalArgumentException("timePeriod");
    }

    if (works == null)
      return 0;
    for (Work work : works) {
      if (work.getFrom() >= countFrom) {
	hoursWorked += Math.abs((work.getLength() / 1000 / 3600));
      }
    }

    return hoursWorked;
  }
}
